/*
* @Author: Someu
* @Date:   2017-12-04 10:08:22
* @Last Modified by:   Someu
* @Last Modified time: 2017-12-04 10:45:50
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('page/common/nav-side/index.js');
require('page/common/index.js');
require('page/index/index.css');

var _user           = require('service/user-service.js');
var _mm             = require('util/mm.js');
var navSide         = require('page/common/nav-side/index.js');


var page = {    
    
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        //点击保存按钮后动作
        $('.btn.btn-submit').click(function(){
            var data = {
                oldPassword         : $.trim($('#old-password').val()),
                newPasword          : $.trim($('#new-password').val()),
                confirmPassword     : $.trim($('#confirm-password').val())
            };
            validateResult = _this.validateForm(data);
            if(validateResult.status){
                _user.updatePassword(data,function(res){
                    _mm.successTips(res);
                    //window.location.href = './user-password-update.html';
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }else{
                _mm.errorTips(validateResult.msg);
            }
        });
    },
    validateForm : function(data){
        var result = {
            status : false,
            msg : ''
        };
        //验证密码长度
        if(data.newPasword.length < 6){
            result.msg = '密码长度不能小于６';
            return result;
        }
        //验证两次密码是否一致
        if(data.newPasword　!== data.confirmPassword){
            result.msg = '两次输入的密码不一致';
            return result;
        }
        
        result.status   = true;
        result.msg      = '验证通过';
        return result;
    },
    onLoad : function(){
        navSide.init({
            name : 'pass-update'
        });
    },
};

$(function(){
    page.init();
});